package yxy.game.pm2.active.command.holiday;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import yxy.apple.util.ArraryMesh;
import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.*;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_节日活动;
import yxy.game.pm2.active.module.DrawStatus;
import yxy.game.pm2.active.module.subordinateclass.LimitGiftPackage;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.bean.ServerBean;
import yxy.game.pm2.bean.task.Task;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ActiveMessage;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.player.FunctionHandle;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * Author HYB
 * 获取节日信息
 */
@MessageType(proto = Command.ActHolidayInfo, classtype = ActiveMessage.ActHolidayInfoRequest.class)
public class CMD_Active_节日_Info extends Command<ActiveMessage.ActHolidayInfoRequest> {

    @Override
    public void execute(MessageSource messageSource, ActiveMessage.ActHolidayInfoRequest request) throws Exception {
        String pcode = messageSource.getTag();
        ActiveMessage.ActHolidayInfoResponse.Builder response = ActiveMessage.ActHolidayInfoResponse.newBuilder();

        try {
            ActiveModule_节日活动 am = ActiveModule_节日活动.get(pcode, false);
            if (am == null) {
                send(messageSource, response, Result.ActNoOpen);
                return;
            }
            if (!am.running()) {
                send(messageSource, response, Result.ActNoOpen);
                return;
            }

            PlayerBean pb = PlayerBean.get(pcode);
            ServerBean sb = ServerBean.get(pb.getServerId());
            ArrayList<Integer> drawDay = am.getDrawDay();
            response.addAllDrawDay(drawDay);
            response.setLogindays(am.getLogindays());
            response.setChallenge(am.getChallenge());
            response.addAllExchanges(make(am.getExchanges()));
            response.addAllIntegratReward(make(am.getIntegratReward()));
            response.setLogindays(am.getLogindays());
            response.setLotteryCount(am.getLotteryCount());
            response.addAllTaskActiveStatus(make(am.getTaskActiveStatus()));
            response.addAllLotteryActiveStatus(make(am.getLotteryActiveDraw()));
            response.addAllAwardsLog(am.getLog(sb.getAreaId()));
            response.setTaskActiveNum(am.getTaskActiveNum());
            Map<String, Task> taskMap = Task.all(pcode);
            List<String> taskCodes = am.getTaskCodes();
            for (String sd : taskCodes) {
                ActiveMessage.TaskInfo.Builder build = ActiveMessage.TaskInfo.newBuilder();
                build.setCode(sd);
                Task task = taskMap.get(sd);
                DrawStatus canget = task.isFinish() ? task.isReward() ? DrawStatus.hadget : DrawStatus.canget : DrawStatus.cannotget;
                build.setTaskid(task.getDataId());
                build.setCanget(canget.getStatus());
                build.setProgress(task.getProgress());
                response.addTasks(build.build());
            }

            Map<String, atcholidayshop2> map = DataCenter.getAllData(atcholidayshop2.class);

            //todo 相隔了几个月(动态增加商品数量 2022.08.26)
            Integer passMonth = FunctionHandle.getAlreadlyPassMonth(pcode);
            config data = DataCenter.getData(4, config.class);
            JSONObject jsonObject = JSONObject.parseObject(data.getValue());
            Integer maxraisetimes = jsonObject.getInteger("maxraisetimes");
            PlayerBean playerBean = PlayerBean.get(pcode);
            Integer begin_server = jsonObject.getInteger("begin_server");


            for (LimitGiftPackage lgp : am.getNewlgps()) {
                ActiveMessage.LimitGiftPackageInfo.Builder build = ActiveMessage.LimitGiftPackageInfo.newBuilder();
                build.setId(lgp.getId());
                build.setGoodsid(lgp.getGoodsid());
                build.setLimit(lgp.getLimit());
                build.setLocknum(lgp.getLockorder().size());
                build.setBuynum(lgp.getBuy());
                int canget = lgp.getBuy() + lgp.getLockorder().size() >= lgp.getLimit() ? DrawStatus.cannotget.getStatus() :
                        DrawStatus.canget.getStatus();
                build.setCanget(canget);
                atcholidayshop2 alp = map.get(String.valueOf(lgp.getId()));
                if (alp == null) {
                    continue;
                }

                int[] num = playerBean.getServerId() >= begin_server ? ArraryMesh.getNum(alp.getRewardnum(), alp.getRaise(), passMonth, maxraisetimes) : alp.getRewardnum();

                Collection<Currency> curs = Currency.create(alp.getRewardtype(), alp.getRewardid(), num);
                Collection<PackageMessage.Item> items = new CurrencyHandle().packItems(curs);
                build.addAllCurs(items);
                response.addLgpinfos(build.build());
            }
            //前端积分奖励，伤害奖励， 兑换商城 ，转盘消耗道具的数据改为从后端返回
            msgToFront(response);

            send(messageSource, response, Result.Success);
        } catch (Exception e) {
            e.printStackTrace();
            send(messageSource, response, Result.DataError);
        }
    }


    //前端积分奖励，伤害奖励， 兑换商城 ，转盘消耗,转盘活跃，日常任务的数据改为从后端返回
    private void msgToFront(ActiveMessage.ActHolidayInfoResponse.Builder response) {
        Map<String, atcholidaypoints> atcholidaypointsList = DataCenter.getAllData(atcholidaypoints.class);//积分奖励
        Map<String, atcholidaypreview> atcholidaypreviewList = DataCenter.getAllData(atcholidaypreview.class);//伤害奖励
        Map<String, atcholidayshop> atcholidayshopList = DataCenter.getAllData(atcholidayshop.class);//兑换商城
        Map<String, atcholidayturntable> atcholidayturntableList = DataCenter.getAllData(atcholidayturntable.class);//转盘消耗道具
        Map<String, atcholidayturntableactive> atcholidayturntableactiveList = DataCenter.getAllData(atcholidayturntableactive.class);//转盘活跃
        Map<String, atcholidayactivecumulate> atcholidayactivecumulateList = DataCenter.getAllData(atcholidayactivecumulate.class);//日常任务

        //封装atcholidaypoints积分奖励数据
        JSONArray atcholidaypointsJsonArrary = getAtcholidaypointsJsonArrary(atcholidaypointsList);

        //封装atcholidaypreview伤害奖励数据
        JSONArray atcholidaypreviewJsonArrary = getAtcholidaypreviewJsonArrary(atcholidaypreviewList);

        //封装atcholidayshop兑换商城数据
        JSONArray atcholidayshopJsonArrary = getAtcholidayshopJsonArrary(atcholidayshopList);

        //封装atcholidayturntable转盘消耗数据
        JSONArray atcholidayturntableJsonArrary = getAtcholidayturntableJsonArrary(atcholidayturntableList);

        //封装atcholidayturntableactive转盘活跃数据
        JSONArray atcholidayturntableactiveJsonArrary = getAtcholidayturntableactiveJsonArrary(atcholidayturntableactiveList);

        //封装atcholidayactivecumulate日常任务数据
        JSONArray atcholidayactivecumulateJsonArrary = getAtcholidayactivecumulateJsonArrary(atcholidayactivecumulateList);

        response.setAtcholidaypoints(atcholidaypointsJsonArrary.toString());
        response.setAtcholidaypreview(atcholidaypreviewJsonArrary.toString());
        response.setAtcholidayshop(atcholidayshopJsonArrary.toString());
        response.setAtcholidayturntable(atcholidayturntableJsonArrary.toString());
        response.setAtcholidayturntableactive(atcholidayturntableactiveJsonArrary.toString());
        response.setAtcholidayactivecumulate(atcholidayactivecumulateJsonArrary.toString());
    }


    //响应
    private void send(MessageSource messageSource, ActiveMessage.ActHolidayInfoResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }

    //封装atcholidayactivecumulate日常任务数据
    private JSONArray getAtcholidayactivecumulateJsonArrary(Map<String, atcholidayactivecumulate> atcholidayactivecumulateList) {
        JSONArray jsonArray = new JSONArray();
        atcholidayactivecumulateList.forEach((k, v) -> {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("id", v.getId());
            jsonObject.put("accumulate", v.getAccumulate());
            jsonObject.put("display", v.getDisplay());
            int[] rewardid = v.getRewardid();
            JSONArray rewardidArrary = new JSONArray();
            for (int i : rewardid) {
                rewardidArrary.add(i);
            }
            int[] rewardnum = v.getRewardnum();
            JSONArray rewardnumArrary = new JSONArray();
            for (int i : rewardnum) {
                rewardnumArrary.add(i);
            }
            int[] rewardtype = v.getRewardtype();
            JSONArray rewardtypeArrary = new JSONArray();
            for (int i : rewardtype) {
                rewardtypeArrary.add(i);
            }
            jsonObject.put("rewardid", rewardidArrary);//道具id组
            jsonObject.put("rewardnum", rewardnum);//道具数量组
            jsonObject.put("rewardtype", rewardtype);//奖励类型组
            jsonArray.add(jsonObject);
        });
        return jsonArray;
    }

    //封装atcholidayturntableactive转盘活跃数据
    private JSONArray getAtcholidayturntableactiveJsonArrary(Map<String, atcholidayturntableactive> atcholidayturntableactiveList) {
        JSONArray jsonArray = new JSONArray();
        atcholidayturntableactiveList.forEach((k, v) -> {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("id", v.getId());
            jsonObject.put("accumulate", v.getAccumulate());
            jsonObject.put("display", v.getDisplay());
            int[] rewardid = v.getRewardid();
            JSONArray rewardidArrary = new JSONArray();
            for (int i : rewardid) {
                rewardidArrary.add(i);
            }
            int[] rewardnum = v.getRewardnum();
            JSONArray rewardnumArrary = new JSONArray();
            for (int i : rewardnum) {
                rewardnumArrary.add(i);
            }
            int[] rewardtype = v.getRewardtype();
            JSONArray rewardtypeArrary = new JSONArray();
            for (int i : rewardtype) {
                rewardtypeArrary.add(i);
            }
            jsonObject.put("rewardid", rewardidArrary);//道具id组
            jsonObject.put("rewardnum", rewardnum);//道具数量组
            jsonObject.put("rewardtype", rewardtype);//奖励类型组
            jsonArray.add(jsonObject);
        });
        return jsonArray;
    }


    //封装atcholidayturntable数据
    private JSONArray getAtcholidayturntableJsonArrary(Map<String, atcholidayturntable> atcholidayturntableList) {
        JSONArray jsonArray = new JSONArray();
        atcholidayturntableList.forEach((k, v) -> {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("id", v.getId());
            jsonObject.put("costId", v.getCostid());//消耗道具id
            jsonObject.put("costnum", v.getCostnum());//消耗道具数量
            jsonObject.put("costtype", v.getCosttype());//消耗道具类型
            jsonObject.put("rewardid", v.getRewardid());//转盘上道具id
            jsonObject.put("rewardnum", v.getRewardnum());//转盘上道具数量
            jsonObject.put("rewardtype", v.getRewardtype());//转盘上道具类型
            jsonArray.add(jsonObject);
        });
        return jsonArray;
    }

    //封装atcholidayshop数据
    private JSONArray getAtcholidayshopJsonArrary(Map<String, atcholidayshop> atcholidayshopList) {
        JSONArray jsonArray = new JSONArray();
        atcholidayshopList.forEach((k, v) -> {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("id", v.getId());
            jsonObject.put("costid", v.getCostid());//消耗道具id
            jsonObject.put("costnum", v.getCostnum());//消耗道具数量
            jsonObject.put("costtype", v.getCosttype());//消耗道具类型
            jsonObject.put("limitnum", v.getLimitnum());//限购数量
            jsonObject.put("limittype", v.getLimittype());//限购类型
            jsonObject.put("rewardid", v.getRewardid());//商品id
            jsonObject.put("rewardnum", v.getRewardnum());//商品数量
            jsonObject.put("rewardtype", v.getRewardtype());//商品类型
            jsonArray.add(jsonObject);
        });
        return jsonArray;
    }

    //封装atcholidaypreview数据
    private JSONArray getAtcholidaypreviewJsonArrary(Map<String, atcholidaypreview> atcholidaypreviewList) {
        JSONArray jsonArray = new JSONArray();
        atcholidaypreviewList.forEach((k, v) -> {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("id", v.getId());
            jsonObject.put("hurt1", v.getHurt1());//上海区间
            int[] rewardid = v.getRewardid();
            JSONArray rewardidArrary = new JSONArray();
            for (int i : rewardid) {
                rewardidArrary.add(i);
            }
            int[] rewardnum = v.getRewardnum();
            JSONArray rewardnumArrary = new JSONArray();
            for (int i : rewardnum) {
                rewardnumArrary.add(i);
            }
            int[] rewardtype = v.getRewardtype();
            JSONArray rewardtypeArrary = new JSONArray();
            for (int i : rewardtype) {
                rewardtypeArrary.add(i);
            }
            jsonObject.put("rewardid", rewardidArrary);//道具id组
            jsonObject.put("rewardnum", rewardnumArrary);//道具数量组
            jsonObject.put("rewardtype", rewardtypeArrary);//奖励类型组
            jsonArray.add(jsonObject);
        });
        return jsonArray;
    }


    //封装atcholidaypoints数据
    private JSONArray getAtcholidaypointsJsonArrary(Map<String, atcholidaypoints> atcholidaypointsList) {
        JSONArray jsonArray = new JSONArray();
        atcholidaypointsList.forEach((k, v) -> {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("id", v.getId());
            jsonObject.put("group", v.getGroup());//积分条件
            int[] rewardid = v.getRewardid();
            JSONArray rewardidArrary = new JSONArray();
            for (int i : rewardid) {
                rewardidArrary.add(i);
            }
            int[] rewardnum = v.getRewardnum();
            JSONArray rewardnumArrary = new JSONArray();
            for (int i : rewardnum) {
                rewardnumArrary.add(i);
            }
            int[] rewardtype = v.getRewardtype();
            JSONArray rewardtypeArrary = new JSONArray();
            for (int i : rewardtype) {
                rewardtypeArrary.add(i);
            }
            jsonObject.put("rewardid", rewardidArrary);//道具id组
            jsonObject.put("rewardnum", rewardnum);//道具数量组
            jsonObject.put("rewardtype", rewardtype);//奖励类型组
            jsonArray.add(jsonObject);
        });
        return jsonArray;
    }


    public Collection<ActiveMessage.MapInfo> make(Map<Integer, Integer> map) {
        List<ActiveMessage.MapInfo> list = new ArrayList<>();
        map.forEach((k, v) -> {
            ActiveMessage.MapInfo.Builder mapInfo = ActiveMessage.MapInfo.newBuilder();
            mapInfo.setKey(k);
            mapInfo.setValue(v);
            list.add(mapInfo.build());
        });
        return list;
    }

}
